package com.example.demo.service.impl;

import com.example.demo.common.constant.CacheConstants;
import com.example.demo.mapper.SysConfigMapper;
import com.example.demo.model.core.PageVo;
import com.example.demo.model.entity.SysConfig;
import com.example.demo.model.form.ConfigForm;
import com.example.demo.model.query.ConfigQuery;
import com.example.demo.model.vo.ConfigVO;
import com.example.demo.service.CacheableAsyncAbstractService;
import com.example.demo.service.CacheableInitService;
import com.example.demo.service.SysConfigService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service
@Primary
@Slf4j
public class SysConfigServiceCacheImpl extends CacheableAsyncAbstractService implements SysConfigService, CacheableInitService {

    @Qualifier("sysConfigServiceImpl")
    @Autowired
    private SysConfigService service;
    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public void add(ConfigForm form) {
        service.add(form);
        insertCache(form.getConfigkey(), form.getConfigvalue());
        scheduleCacheOperation(() -> insertCache(form.getConfigkey(), form.getConfigvalue()));
    }

    @Override
    public void update(ConfigForm form) {
        service.update(form);
        insertCache(form.getConfigkey(), form.getConfigvalue());
        scheduleCacheOperation(() -> insertCache(form.getConfigkey(), form.getConfigvalue()));
    }

    @Override
    public void delete(List<String> ids) {
        for (String id : ids) {
            ConfigForm form = this.form(id);
            deleteCache(form.getConfigkey());
            scheduleCacheOperation(() -> deleteCache(form.getConfigkey()));
        }
        service.delete(ids);
    }

    @Override
    public PageVo<ConfigVO> page(ConfigQuery query) {
        return service.page(query);
    }

    @Override
    public ConfigForm form(String id) {
        return service.form(id);
    }

    @Override
    public String getConfigValueByKey(String configKey) {
        String key = CacheConstants.CacheKey.CONFIG + configKey;
        String cached = Objects.requireNonNull(cacheManager.getCache(CacheConstants.CacheName.SYSTEM))
                .get(key, String.class);
        if (Objects.nonNull(cached)) {
            return cached;
        }
        String value = service.getConfigValueByKey(configKey);
        if (Objects.nonNull(value) && !value.isEmpty()) {
            insertCache(configKey, value);
        }
        return Objects.nonNull(value) ? value : "";
    }

    @Override
    @PostConstruct
    public void initCache() {
        Objects.requireNonNull(cacheManager.getCache(CacheConstants.CacheName.SYSTEM)).clear();
        List<SysConfig> sysConfigs = sysConfigMapper.selectAll();
        for (SysConfig sysConfig : sysConfigs) {
            Objects.requireNonNull(cacheManager.getCache(CacheConstants.CacheName.SYSTEM)).putIfAbsent(CacheConstants.CacheKey.CONFIG + sysConfig.getConfigkey(), sysConfig.getConfigvalue());
        }
    }


    private void deleteCache(String key) {
        Objects.requireNonNull(cacheManager.getCache(CacheConstants.CacheName.SYSTEM)).evictIfPresent(CacheConstants.CacheKey.CONFIG + key);
    }

    private void insertCache(String key, String value) {
        Objects.requireNonNull(cacheManager.getCache(CacheConstants.CacheName.SYSTEM)).put(CacheConstants.CacheKey.CONFIG + key, value);
    }

}




